package controllers;

import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import domain.Event;
import domain.Participant;

import services.EventService;
import services.ParticipantService;

@Controller
@RequestMapping("/event")
public class EventController extends AbstractController {

	// Constructors -----------------------------------------------------------

	public EventController() {
		super();
	}

	// Services ---------------------------------------------------------------

	@Autowired
	private EventService eventService;
	
	@Autowired
	private ParticipantService participantService;

	// List events ------------------------------------------------------------

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ModelAndView list() {
		ModelAndView result;
		Collection<Event> events;

		events = eventService.findFutureEvents();
		Participant p;
		
		try{
			p = participantService.findByPrincipal();
		}
		catch(Exception ex){
			p=null;
		}
		
		
		result = new ModelAndView("event/list");
		result.addObject("requestURI", "event/list.do");
		if(p!=null)
		{
			Collection<Event> events2= eventService.findEventsByParticipant();
			events.removeAll(events2);
			result.addObject("registrable", true);
		}
		result.addObject("events", events);
		result.addObject("sortable", true);
	

		return result;
	}

	// List on-going events ---------------------------------------------------

	@RequestMapping(value = "/on-goinglist", method = RequestMethod.GET)
	public ModelAndView onGoingList() {
		ModelAndView result;
		Collection<Event> events;

		events = eventService.findOngoingEvents();
		result = new ModelAndView("event/on-goinglist");
		result.addObject("requestURI", "event/on-goinglist.do");
		result.addObject("events", events);
		result.addObject("sortable", true);

		return result;
	}
	
	// List of past events ---------------------------------------------------

		@RequestMapping(value = "/pastlist", method = RequestMethod.GET)
		public ModelAndView pastList() {
			ModelAndView result;
			Collection<Event> events;

			events = eventService.findPastEvents();
			result = new ModelAndView("event/pastlist");
			result.addObject("requestURI", "event/pastlist.do");
			result.addObject("events", events);
			result.addObject("sortable", false);

			return result;
		}
}
